package com.zhu.storesmanager.views;

import com.zhu.storesmanager.R;
import com.zhu.storesmanager.SvVisitTaskActivity;
import com.zhu.storesmanager.models.VisitTaskInfo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Check弹窗界面
 */
public class PopCheckView {

    private FrameLayout mContentView = null; //父View
    private View mPopView = null; //谈窗界面
    private SvVisitTaskActivity mSupervisorVisitTaskActivity = null;

    /**
     * @param layout 需要添加文本弹窗的父界面
     * 
     */
    public PopCheckView(SvVisitTaskActivity activity, FrameLayout layout){
    	mSupervisorVisitTaskActivity = activity;
        mContentView = layout;
        mPopView = LayoutInflater.from(mContentView.getContext()).inflate(R.layout.layout_popdialog_check, null);
        mPopView.setOnClickListener(mListener);
        ImageView close = (ImageView) mPopView.findViewById(R.id.checkpop_close);
        close.setOnClickListener(mListener);
    }

    /**
     * 显示弹窗界面
     * @param info 门店信息
     * 
     */
    public void showPopTextView(VisitTaskInfo info){
        TextView titleView = (TextView) mPopView.findViewById(R.id.checkpop_title);
        titleView.setText(info.mName);
        Button checkin = (Button) mPopView.findViewById(R.id.checkpop_checkin);
        Button checkout = (Button) mPopView.findViewById(R.id.checkpop_checkout);
        Button visit = (Button) mPopView.findViewById(R.id.checkpop_visitcontent);
        checkin.setTag(info);
        checkout.setTag(info);
        visit.setTag(info);
        if(info.mCheckinTime > 0){ //已经签入
        	setButtonEnable(checkin, false, R.drawable.checkin_gray);
        	setButtonEnable(visit, true, R.drawable.visit_content);
        	if(info.mCheckoutTime > 0){ //已经签出
                setButtonEnable(checkout, false, R.drawable.checkout_gray);
            }else{
                setButtonEnable(checkout, true, R.drawable.checkout);
            }
        }else{ //没有签入
        	setButtonEnable(checkout, false, R.drawable.checkout_gray);
            setButtonEnable(checkin, true, R.drawable.checkin);
            setButtonEnable(visit, false, R.drawable.visit_content_gray);
        }
        if(mContentView.indexOfChild(mPopView) == -1){
        	mContentView.addView(mPopView);
        }
    }

    /**
     * 关闭弹窗界面
     */
    public void closePopTextView(){
        if(mContentView.indexOfChild(mPopView) != -1){
            mContentView.removeView(mPopView);
        }
    }

    /**
     * 处理返回键
     * @return true 表示截获返回键处理弹窗消息
     */
    public boolean doBack(){
        if(mContentView.indexOfChild(mPopView) != -1){
            mContentView.removeView(mPopView);
            return true;
        }
        return false;
    }

    /**
     * 点击时间监听
     */
    private OnClickListener mListener = new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v != null){
                if(v.getId() == R.id.checkpop_close){ //点击关闭
                    closePopTextView();
                }else if(v.getId() == R.id.checkpop_checkin){ //签入
                	if(mSupervisorVisitTaskActivity.startCheck(SvVisitTaskActivity.CHECK_IN)){
                		closePopTextView();
                	}
                }else if(v.getId() == R.id.checkpop_checkout){//签出
                	if(mSupervisorVisitTaskActivity.startCheck(SvVisitTaskActivity.CHECK_OUT)){
                		closePopTextView();
                	}
                }else if(v.getId() == R.id.checkpop_visitcontent){ //拜访内容
                    mSupervisorVisitTaskActivity.gotoVisitContentActivity((VisitTaskInfo)v.getTag());
                    closePopTextView();
                }
            }
        }
    };

    /**
     * 设置按钮状态
     * @param button 按钮
     * @param isEnabled 开启
     * @param resId 图片资源ID
     */
    private void setButtonEnable(Button button, boolean isEnabled, int resId){
        if(isEnabled){
            button.setEnabled(true);
            button.setCompoundDrawablesWithIntrinsicBounds(null, mSupervisorVisitTaskActivity.getResources().getDrawable(resId), null, null);
            button.setTextColor(0xff349a30);
            button.setOnClickListener(mListener);
        }else{
            button.setEnabled(false);
            button.setCompoundDrawablesWithIntrinsicBounds(null, mSupervisorVisitTaskActivity.getResources().getDrawable(resId), null, null);
            button.setTextColor(0xffb0aeae);
            button.setOnClickListener(null);
        }
    }
}
